package com.javaweb.service.impl;

import com.javaweb.aop.GranularType;
import com.javaweb.aop.TimeMeasure;
import com.javaweb.dao.UserDao;
import com.javaweb.model.User;
import com.javaweb.service.UserService;
import com.javaweb.service.dto.PageResult;
import com.javaweb.service.dto.UserConditionDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/9/23 12:36
 */
@TimeMeasure(granular = GranularType.MIL_SECOND)
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao ;
    @Override
    public User login(String username, String password) {
        User user = new User() ;
        if( user.getName().equals( "123")) {

        }
        return userDao.selectOne(username, password) ;
    }

    @Override
    public List<User> findUserByName(String username) {
        return null;
    }

//    @Override
//    public List<User> findUserByName(String username) {
//        User user = new User() ;
//        if( user.getName().equals( "123")) {
//
//        }
//        return userDao.selectByName(username) ;
//    }

    @Override
    public User queryByName(String username) {

        return userDao.selectByName(username) ;
    }

//    @Override
//    public List<User> queryUserByName(String username) {
//        return null;
//    }

    @Override
    public int register(User user) {
        int ret = 0;
        if(user.getName() == null || user.getName() == "")
        {
            return ret;
        }

        else
        {
            return userDao.insert(user);
        }

    }

    @Override
    public PageResult queryUsers(UserConditionDto userCondition, Integer pageNum, Integer pageSize)
    {

        PageResult pageResult = new PageResult().setData(userDao.selectByUserCondition(userCondition))
                .setPageNum(pageNum)
                .setPageSize(pageSize);
        return pageResult;
    }

    @Transactional
    @Override
    public void addAssignRole(String userId, List roleIds) {

        if(!roleIds.isEmpty()){
            int i=1;
            int ret = userDao.insertUserRole(Integer.valueOf(userId), Integer.valueOf((String) roleIds.get(0)));
            while (ret>0 && i<roleIds.size()){
                ret = userDao.insertUserRole(Integer.valueOf(userId), Integer.valueOf((String) roleIds.get(i)));
                i++;
            }
        }

    }
}
